package com.thread;

/**
 * @author liliang
 * @date 2019/12/19.
 */
public class ThreadLocalDemo {

    private static ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<String>(){
        @Override
        protected String initialValue() {
            return "调用initialValue方法初始化的值";
        }
    };


    public static void main(String[] args) {
        THREAD_LOCAL.set("与 main 线程关联的字符串");
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1线程从ThreadLocal中获取值:"+ THREAD_LOCAL.get());
                THREAD_LOCAL.set("与 t1 线程关联的字符串");
                System.out.println("再从t1线程ThreadLocal中获取值:"+ THREAD_LOCAL.get());
            }
        }, "t1").start();
        System.out.println("main 线程从 ThreadLocal中获取值:" + THREAD_LOCAL.get());
    }
}
